package model;

import com.VanDoren.ArgusV2.R;
import com.google.zxing.Result;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ThreadHandler extends Handler
{
	private static final String TAG = ThreadHandler.class.getSimpleName();
	
    private OnThreadResponseCallback callback;

    public ThreadHandler(OnThreadResponseCallback callback) {
        this.callback = callback;
    }

    /*
     * this method handles the receiving messages and translates the data that is sent along with the message
     */
	@Override
    public void handleMessage(Message msg) {
    	Object data = msg.obj;
    	
    	switch(msg.what)
    	{
    	case R.id.newMachineReceived:
    		Log.d(TAG, String.format("MachineReceived: %s", data.toString()));
    		
    		callback.onNewMachineReceivedFromConnection((Machine)data);
    		break;
    	case R.id.updatedVarsForMachineReceived:
    		
    		callback.onUpdatedInfoFromMachine((Machine)data);
    		break;
    	case R.id.connectionFailed:
    		callback.onConnectionFailed((String)data);
    		break;
    	case R.id.translationFailed:
    		callback.onTranslationFailed((String)data);
    		break;
		case R.id.decodeSuccess:
			Result[] results = (Result[]) data;
			callback.onDecoded(results);
			break;
		case R.id.decodeFailed:
			callback.onDecodeFailed("Decoding failed");
			break;
		case R.id.nonExistantMachine:
			callback.onNonExistantMachine((String)data);
			break;
    		default:
    			// do nothing
    			break;
    		
    	}
    	
    }

	// this interface shows what methods should be implemented on the receiving end of the handler messages
    public static interface OnThreadResponseCallback 
    {
    	void onNewMachineReceivedFromConnection(Machine machine);
    	void onNonExistantMachine(String data);
		void onUpdatedInfoFromMachine(Machine machine);
    	void onConnectionFailed(String ID);
    	void onTranslationFailed(String ID);
    	void onDecoded(Result[] results);
    	void onDecodeFailed(String data);
    }
}
